package com.yellow.cloud.system.controller;

import com.yellowframework.cloud.api.system.SysDataModelControllerApi;
import com.yellowframework.cloud.domain.system.SysDataModel;
import com.yellowframework.cloud.domain.system.ext.SysDataModelExt;
import com.yellow.cloud.system.service.SysDataModelService;
import com.yellowframework.cloud.common.model.response.ObjectResponseResult;
import com.yellowframework.cloud.common.model.response.QueryResponseResult;
import com.yellowframework.cloud.common.model.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;

@RestController
@RequestMapping("sys/datamodel")
public class SysDataModelController implements SysDataModelControllerApi {

    @Autowired
    private SysDataModelService sysDictionaryService;

    @Override
    @PreAuthorize("hasAuthority('sys:datamodel:list')")
    @GetMapping(value = "/page/list/{page}/{size}")
    public QueryResponseResult<SysDataModel> pageList(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return sysDictionaryService.pageList(page, size);
    }

    @Override
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('sys:datamodel:info')")
    public ObjectResponseResult<SysDataModelExt> get(@PathVariable("id") Integer id){
        return ObjectResponseResult.success(sysDictionaryService.findById(id, true));
    }

    @Override
    @PostMapping
    @PreAuthorize("hasAuthority('sys:datamodel:save')")
    public ResponseResult save(@Valid @RequestBody SysDataModelExt request){
        sysDictionaryService.add(request);
//        sysDictionaryService.addDm(request);
        return ResponseResult.success();
    }

    @Override
    @PutMapping("{id}")
    @PreAuthorize("hasAuthority('sys:datamodel:update')")
    public ResponseResult update(@PathVariable("id") Integer id, @Valid @RequestBody SysDataModelExt request){
        sysDictionaryService.update(id, request.getModelType(), request);

        return ResponseResult.success();
    }

    @Override
    @DeleteMapping
    @PreAuthorize("hasAuthority('sys:datamodel:delete')")
    public ResponseResult delete(@RequestBody Long[] ids){
        sysDictionaryService.removeByIds(Arrays.asList(ids));

        return ResponseResult.success();
    }

}